package com.qiangqiang.task.server.exception.handler;

import com.qiangqiang.task.server.exception.TaskException;
import com.qiangqiang.task.server.exception.UserException;
import com.qiangqiang.task.server.util.ResultVOUtil;
import com.qiangqiang.task.server.vo.ResultVO;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: task
 * @description:
 * @author: Sky.lin
 * @create: 2019-02-18 17:03
 **/
@ControllerAdvice
@ResponseBody
public class ExceptionHandler {

    @org.springframework.web.bind.annotation.ExceptionHandler(value = Exception.class)
    public ResultVO handle(Exception e) {
        if (e instanceof UserException) {
            UserException userException = (UserException) e;
            return ResultVOUtil.error(userException.getMessage());
        } else if (e instanceof TaskException) {
            TaskException taskException = (TaskException) e;
            return ResultVOUtil.error(taskException.getMessage());
        } else {
            System.out.println(e);
            return ResultVOUtil.error("未知错误");
        }
    }
}
